package com.sinochem.yunlian.ship.base.rest;

import com.github.pagehelper.Page;
import com.sinochem.yunlian.ship.api.vo.uc.UcUserInfo;
import com.sinochem.yunlian.ship.base.biz.BsDataDictItemBiz;
import com.sinochem.yunlian.ship.base.biz.BsPortalNewBiz;
import com.sinochem.yunlian.ship.base.biz.BsProtalIndexBiz;
import com.sinochem.yunlian.ship.base.entity.BsDataDictItem;
import com.sinochem.yunlian.ship.base.entity.BsPortalIndex;
import com.sinochem.yunlian.ship.base.entity.BsPortalNews;
import com.sinochem.yunlian.ship.common.annotation.Subject;
import com.sinochem.yunlian.ship.base.vo.request.FindPortalNewVo;
import com.sinochem.yunlian.ship.base.vo.request.PortalNewsVo;
import com.sinochem.yunlian.ship.base.vo.response.BsPortalIndexDetailVo;
import com.sinochem.yunlian.ship.common.annotation.IgnoreToken;
import com.sinochem.yunlian.ship.common.msg.RestResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by jayway on 2017/12/9.
 */
@IgnoreToken
@RestController
@RequestMapping("/om/portal")
public class BsPortalNewController {

    @Autowired
    private  BsPortalNewBiz newBiz;
    @Autowired
    private BsProtalIndexBiz protalIndexBiz;
    @Autowired
    private BsDataDictItemBiz bsDataDictItemBiz;

    /**
     * 运营管理-首页添加新闻
     * @param news
     * @return
     */
    @RequestMapping("/addNews")
    public RestResponse addNews(@RequestBody PortalNewsVo news, @Subject(required = false) UcUserInfo userInfo){
        newBiz.addNews(news,userInfo);
        return RestResponse.success();
    }

    /**
     * 运营管理-修改news
     * @param news
     * @return
     */
    @RequestMapping("/updateNews")
    public RestResponse updateNews(@RequestBody PortalNewsVo news,@Subject(required = false) UcUserInfo userInfo){
        newBiz.updateNews(news,userInfo);
        return RestResponse.success();
    }
    /**
     * 首页新闻列表
     * @param vo
     * @return
     */
    @RequestMapping("/newsList")
    public RestResponse findNewsList(@RequestBody  FindPortalNewVo vo)  {
        Page<BsPortalNews> data = newBiz.findNewsList(vo);
        Map result = new HashMap();
        result.put("rows",data.getResult());
        result.put("total",data.getTotal());
        return RestResponse.success(result);
   }

    /**
     * 获取最新统计数据
     * @return
     */
    @RequestMapping("/statisticsData")
    public RestResponse statisticNum(){
        BsPortalIndexDetailVo vo = new BsPortalIndexDetailVo();
        BsPortalIndex newestInfo = protalIndexBiz.getLastInfo();
        BeanUtils.copyProperties(newestInfo,vo);
        List<BsDataDictItem> items = bsDataDictItemBiz.findByDictCodeForView("StatisticsShipMeteria");
        String vshipEmpty= items.stream().filter(e->"ShipEmpty".equals(e.getItemCode())).collect(Collectors.toList()).get(0).getItemValue();
        String vship= items.stream().filter(e->"ship".equals(e.getItemCode())).collect(Collectors.toList()).get(0).getItemValue();
        String vmaterial= items.stream().filter(e->"Material".equals(e.getItemCode())).collect(Collectors.toList()).get(0).getItemValue();
        String vTotalShipEmpty= items.stream().filter(e->"totalShipEmpty".equals(e.getItemCode())).collect(Collectors.toList()).get(0).getItemValue();
        vo.setVmaterial(Integer.valueOf(vmaterial));
        vo.setVTotalShipEmpty(Integer.valueOf(vTotalShipEmpty));
        vo.setVship(Integer.valueOf(vship));
        vo.setVshipEmpty(Integer.valueOf(vshipEmpty));
       return RestResponse.success(vo);
    }
    @RequestMapping("/anon/statisticsData")
    public RestResponse statisticNumPc(){
        BsPortalIndexDetailVo vo = new BsPortalIndexDetailVo();
        BsPortalIndex newestInfo = protalIndexBiz.getLastInfo();
        BeanUtils.copyProperties(newestInfo,vo);
        return RestResponse.success(vo);
    }
    /**
     *
     * 设置统计变量，加权数。
     * @return
     */
    @RequestMapping("/setVariable")
    public RestResponse setVariable(@RequestBody Map params){

        List<BsDataDictItem> items = bsDataDictItemBiz.findByDictCodeForView("StatisticsShipMeteria");
        //shipempty,material,ship
        for(BsDataDictItem itemVo: items){
            if("ShipEmpty".equals(itemVo.getItemCode())){
                String se = params.get("shipEmpty")+"";
                itemVo.setItemValue(se);
                bsDataDictItemBiz.updateById(itemVo);
                continue;
            }
            if("Material".equals(itemVo.getItemCode())){
                String se = params.get("material")+"";
                itemVo.setItemValue(se);
                bsDataDictItemBiz.updateById(itemVo);
                continue;
            }
            if("ship".equals(itemVo.getItemCode())){
                String se = params.get("ship")+"";
                itemVo.setItemValue(se);
                bsDataDictItemBiz.updateById(itemVo);
                continue;
            }
        }
        BsDataDictItem item = items.stream().filter(e->e.getItemCode().equals("totalShipEmpty")).collect(Collectors.toList()).get(0);
        item.setItemValue(params.get("totalShipEmpty")+"");
        bsDataDictItemBiz.updateById(item);
        return RestResponse.success();
    }


}
